using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Reflection;

using GURUCORE.Framework.Core;
using GURUCORE.Framework.Core.Data;
using GURUCORE.Framework.DataAccess.Provider;
using GURUCORE.Framework.DataAccess.ORMapping.CriteriaMapping;
//using GURUCORE.Framework.Core.Indexing;
//using GURUCORE.Framework.DataAccess.Indexing;
// using System.Data.SqlClient;


namespace GURUCORE.Framework.DataAccess.ORMapping
{
    /// <summary>
    /// /// ONLY WORK WITH SQL SERVER DATABASE
    /// </summary>
    [Obsolete("This class added by TrungDD from Procon")]
    public sealed class TableMapperSP : IExpressionMaker
    {
         string m_sGMetaTable;
         string m_sKeyField;
         Type m_tDTOType;

        public TableMapperSP()
        {
        }

        public TableMapperSP(string p_sGMetaTable)
        {
            m_sGMetaTable = p_sGMetaTable;
        }

        public TableMapperSP(string p_sGMetaTable, string p_sKeyField)
        {
            m_sGMetaTable = p_sGMetaTable;
            m_sKeyField = p_sKeyField;
        }

        public TableMapperSP(Type p_Type)
        {
            m_tDTOType = p_Type;
        }


        #region IExpressionMaker Members

        public string FormatStringConstant(string p_sInput)
        {
            return DataAccessContext.GetDataAccessContext().GetUnicodeForm().Replace("value", p_sInput.Replace("'", "''"));
        }

        #endregion

        public DTOBase[] _RunSP(SPBase p_oSPBase, Type p_ReturnDTOType)
        {
            p_oSPBase.Read();

            DataAccessContext oDACtx = DataAccessContext.GetDataAccessContext();

            //obtain connection
            IDbConnection oDbConn = oDACtx.GetConnection();

            //do query
            IDbCommand oDbCmd = oDACtx.GetDbCommand(oDbConn);
            oDbCmd.CommandText = p_oSPBase.CommandText;
            oDbCmd.CommandType = CommandType.StoredProcedure;
            foreach (IDataParameter oParam in p_oSPBase.Parameters)
            {
                oDbCmd.Parameters.Add(oParam);
            }

            IDataReader oDataReader;
            DTOBase[] arrDTOs = null;

            try
            {
                //If Execute Reader: return result set collection
                if (p_oSPBase.ExecuteMode == SPBase.ProcedureExecuteMode.ExecuteReader)
                {
                    oDataReader = oDbCmd.ExecuteReader();
                    p_oSPBase.Write();
                    //if is concrete DTO, convert IDbDataReader into DTO
                    if (!p_ReturnDTOType.Equals(typeof(GMetaDTO)))
                    {
                        arrDTOs = DataTransferObjectMaker.MakeDTO(p_ReturnDTOType, oDataReader);
                    }
                    //else, convert to GMetaDTO
                    else
                    {
                        arrDTOs = DataTransferObjectMaker.MakeGMetaDTO(m_sGMetaTable, oDataReader); //, p_arrColumn);
                    }

                    oDataReader.Close();
                }
                //If ExecuteNonQuery: return OUT param values
                else if (p_oSPBase.ExecuteMode == SPBase.ProcedureExecuteMode.ExecuteNonQuery)
                {
                    int iResult = oDbCmd.ExecuteNonQuery();
                    p_oSPBase.Write();
                }
            }
            catch (DbException ex)
            {
                GException.ProcessException(ex); // throw ex;
            }
            finally
            {
                oDACtx.ReturnConnection(oDbConn);
            }

            //return
            return arrDTOs;
        }

    }
}
